/*
 * Copyright 2013 Hongxin Telecommunication Technologies Co, Ltd.,
 * Wuhan, Hubei, China. All rights reserved.
 */
package com.hxrd.interaction;

import com.hxrd.data.Device;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import static com.hxrd.interaction.InterActive.*;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author piqianghua Created on 2017-1-5, 10:13:20
 */
public class BatchImport
{
	private static Log log = LogFactory.getLog(BatchImport.class);
	private static final String path
			= "导入文件.xls";
	private int count;
	private DBManage db = new DBManage();
	HashMap<Integer, Device> listData = new HashMap<Integer, Device>();
	private ArrayList<Device> ad = new ArrayList<Device>();

	/*
	 * 功能：解析文本文件
	 */
	public void loadFile()
	{
		try
		{
			importRecord(path);
			if(db.insertDB(listData) > 0)
			{
				System.out.println("导入设备成功！");
				System.out.println("共有合法的记录" + count + "条");
				listData.clear();
				ad.clear();
				count = 0;
			}
		}
		catch(Exception e)
		{
			System.err.println("导入错误！");
			log.error("导入路径错误！");
			log.info(getTrace(e));
		}
	}

	public void importRecord(String path) throws FileNotFoundException, IOException
	{
		ad = db.getDB();
		HSSFWorkbook workBook = null;
		FileInputStream is = null;
		try
		{
			is = new FileInputStream(path);
		}
		catch(IOException ex)
		{
			System.err.println("文件路径错误!");
			log.error("文件路径错误");
			log.info(getTrace(ex));
			return;
		}
		workBook = new HSSFWorkbook(is);
		HSSFSheet sheet = workBook.getSheetAt(0);
		HSSFRow row;
		if(sheet == null)
		{
			return;
		}
		if(sheet.getPhysicalNumberOfRows() < 2)
		{
			return;
		}
		for(int i = 1; i < sheet.getPhysicalNumberOfRows(); i++)
		{
			Device item = new Device();
			row = sheet.getRow(i);
			if(!getStringCellValue(row.getCell(0)).trim().isEmpty() && !getStringCellValue(row.
					getCell(0)).trim().equals("新增"))
			{
				item.setAreaId(Iarea.get(getStringCellValue(row.getCell(0))));
			}
			else
			{
				if(i == 1)
				{
					System.err.println("导入文件有误，请检查！");
					log.info("导入文件有误，请检查！");
					return;
				}
				else
				{
					item.setAreaId(listData.get(i - 1).getAreaId());
				}
			}
			if(item.getAreaId() == 0)
			{
				System.err.println("第" + i + "行 地市表中不存在此地市,请添加！");
				log.info("第" + i + "行 地市表中不存在此地市,请添加！");
				return;
			}
			if(!getStringCellValue(row.getCell(1)).isEmpty())
			{
				item.setVenderId(Ifactory.
						get(getStringCellValue(row.getCell(1))));

			}
			else
			{
				if(i == 1)
				{
					item.setVenderId(1);
				}
				else
				{
					item.setVenderId(listData.get(i - 1).getVenderId());
				}
			}
			if(item.getVenderId() == 0)
			{
				System.err.println("第" + i + "行 厂家表中不存在此厂家,请添加！");
				log.info("第" + i + "行 厂家表中不存在此厂家,请添加！");
				return;
			}
			if(!getStringCellValue(row.getCell(2)).trim().isEmpty())
			{
				item.setModelId(Ideviceno.
						get(getStringCellValue(row.getCell(2))));

			}
			else
			{
				if(i == 1)
				{
					System.err.println("导入文件有误，请检查！");
					log.info("导入文件有误，请检查！");
					return;
				}
				else
				{
					item.setModelId(listData.get(i - 1).getModelId());
				}

			}
			if(item.getModelId() == 0)
			{
				System.err.println("第" + i + "行 设备型号表中不存在此设备型号,请添加！");
				log.info("第" + i + "行 设备型号表中不存在此设备型号,请添加！");
				return;
			}
			item.setNumberId(getStringCellValue(row.getCell(3)));
		     for(Map.Entry<Integer, Device> entry : listData.entrySet())
			{
				if(entry.getValue().getIp().equals(getStringCellValue(row.
						getCell(4))))
				{
					System.err.println("第" + i + "行" + "IP重复，无法导入！");
					log.info("第" + i + "行" + "IP重复，无法导入！");
					return;
				}
			}
			for(Device newitem : ad)
			{
				if(newitem.getIp().equals(getStringCellValue(row.getCell(4))))
				{
					System.err.println("第" + i + "行" + "IP重复，无法导入！");
					log.info("第" + i + "行" + "IP重复，无法导入！");
					return;
				}
			}
			if(getStringCellValue(row.getCell(4)).
					isEmpty())
			{
				System.err.println("第" + i + "行" + "IP为空，无法导入！");
				log.info("第" + i + "行" + "IP为空，无法导入！");
				return;
			}
			if(!db.Regex(getStringCellValue(row.getCell(4)), 1))
			{
				System.err.println("第" + i + "行" + "IP非法，无法导入！");
				log.info("第" + i + "行" + "IP非法，无法导入！");
				return;
			}
			item.setIp(getStringCellValue(row.getCell(4)));
			if(getStringCellValue(row.getCell(5)).equals("主"))
			{
				item.setMasterId(1);
			}
			else if(getStringCellValue(row.getCell(5)).equals("备"))
			{
				item.setMasterId(2);
			}
			if(!getStringCellValue(row.getCell(6)).trim().isEmpty())
			{
				item.setNasip(getStringCellValue(row.getCell(6)));

			}
			else
			{
				if(i == 1)
				{
					item.setNasip("");
				}
				else
				{
					item.setNasip(listData.get(i - 1).getNasip());
				}
			}
			item.setUsername(getStringCellValue(row.getCell(7)));
			item.setPassword(getStringCellValue(row.getCell(8)));
			if(getStringCellValue(row.getCell(9)).
					isEmpty())
			{
				item.setCommunity("sbzg4321");
			}
			else
			{
				item.setCommunity(getStringCellValue(row.getCell(9)));
			}
			if(!getStringCellValue(row.getCell(10)).
					isEmpty())
			{
				item.setPort(Integer.
						parseInt(getStringCellValue(row.getCell(10))));
			}
			if(!getStringCellValue(row.getCell(11)).
					isEmpty())
			{
				item.setVersion(Integer.parseInt(getStringCellValue(row.
						getCell(11))));
			}
			listData.put(i, item);
			count++;
		}
	}

	private String getStringCellValue(HSSFCell cell)
	{
		if(cell == null)
		{
			return "";
		}
		try
		{
			switch(cell.getCellType())
			{
				case Cell.CELL_TYPE_STRING:
					return cell.getRichStringCellValue().getString().trim();
				case Cell.CELL_TYPE_NUMERIC:
					return (String.valueOf(cell.getNumericCellValue())).
							split("\\..")[0];
				case Cell.CELL_TYPE_BOOLEAN:
					return String.valueOf(cell.getBooleanCellValue());
				case Cell.CELL_TYPE_ERROR:
					return String.valueOf(cell.getErrorCellValue());
				case Cell.CELL_TYPE_FORMULA:
					return cell.getCellFormula();
				case Cell.CELL_TYPE_BLANK:
					return "";
				default:
					return cell.getStringCellValue().trim();
			}
		}
		catch(Exception exp)
		{
			log.info(getTrace(exp));;
			return null;
		}
	}

}
